<template>
  <div>
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      :default-index="value"
      @change="onGenderChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";
export default {
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      columns: ["男", "女"],
      localgender:this.value
    };
  },
  methods: {
    async onConfirm(){
        this.$toast.loading({
            message:'保存中.......',
            forbidClick:true //禁止点击
        })
        try{
            const  gender=this.localgender
            await updateUserProfile({
                gender: gender
            })
            // 更新视图
            this.$emit('input',this.localgender)
            // 关闭弹层
            this.$emit('close')
            // 提示成功
            this.$toast.success('更新成功')
        }catch(err){
            // 更新失败
            this.$toast.fail('更新失败')
            console.log(err)
        }
    },
     onGenderChange(picker, value, index) {
       this.localgender=index
    }
  },
};
</script>

<style>
</style>